$(function () {
    /*
    * 键盘事件*/
    $(document).keydown(function(event){
        var e = event || window.event;
        var k = e.keyCode || e.which;
        switch(k) {
            case 13:
                if($("#userId").is(":focus")||$("#password").is(":focus")||$("#verifier").is(":focus")){
                    $(".loginBtn").trigger("click");
                }
        }

    })
    /*
    * 根据COOKIE设置用户名密码*/
    var c_start=document.cookie.indexOf("user");
    if(c_start>=0){
        var _user=getCookieValue("user");
        $("#userId").val(_user);
    }
    /*
    @ urlRoot:定义根目录
    @ tempHhtml:底部广告UL的一个ITEM html
    @ ajax 渲染底部广告ul li
    */
    var urlRoot = window.document.location.host + window.document.location.pathname;
    var tempHtml = '<div id=""><a href="[@]"><img src="[@@]" width="330" height="150"><span class="activity-txt">[@@@]</span></a></div>';
    $.ajax({
        url: 'fm/json/activity.json',
        data: '',
        async: false,
        success: function (jdata) {
            $(jdata.data).each(function (i, item) {
                var setHtml = tempHtml.replace("[@]", "#");
                setHtml = setHtml.replace("[@@]", "upload/" + item.img);
                setHtml = setHtml.replace("[@@@]", item.text);
                $(".activity-nav").append($(setHtml));
            });
        }
    });
    /*
     登录按钮事件
     */
    $('.loginBtn').bind('click', function () {
        var $logBtn = $(this), $warp = $logBtn.parents('.form-login'), $account = $('#userId'), $password = $('#password'), $verifyText = $('#verifier');
        if ($logBtn.hasClass("dis"))
            return;
//        todo 防止重复提交
//        $logBtn.addClass("dis");
        if ($account.val().trim() == "" || $account.val().length == 0) {
            showLogTip(132, "请输入正确的帐号");
            $account.focus();
            return;
        }
        if ($password.val().trim() == "" || $password.val().length == 0) {
            showLogTip(178, "请输入正确的密码");
            $password.focus();
            return;
        }
        if ($verifyText.val().trim() == "" || $verifyText.val().length == 0) {
            showLogTip(228, "请输入正确的验证码");
            $verifyText.focus();
            return;
        }
        $.ajax({
            url: "checkCode/chk_code.php?act=char",
            data: {code: $verifyText.val()},
            async: false,
            type:"post",
            success: function (msg) {

                if (parseInt(msg) == 1) {
                    var md5_pass= $.md5($("#password").val());
                    $("#password").val(md5_pass);
                    var param = $warp.serialize();
                    $.ajax({
                     url: fm.config.root+'/login/login.php',
                     type: 'post',
                     data: param,
                     success: function (data) {
                         var usID=$("#userId").val(),oData=eval("("+data+")");
                         if(oData.status!=="success"){
                             showLogTip(132, oData.message);
                             $logBtn.removeClass("dis");
                         }else{
                             setCookie("user",oData.data,24*30);
                             setCookie("islogin",1,24);
                             window.location.href=fm.config.root+"fm/account/manage.html";
                         }

                     }
                     });
                    return ;
                }
                showLogTip(228, "请输入正确的验证码");
                return ;
            }
        });

    });
    /*更新验证码*/
    function updateVerImg() {
        $("#getCodeChar").attr("src", 'checkCode/code_char.php?' + Math.random());
    }

    $("#getCodeChar").click(function () {
        updateVerImg();
        return;
    });
    $("#aVerify").click(function () {
        updateVerImg();
        return;
    });
    /*
     设置底部广告
     */
    var $activity = $('.activity-nav'), $itemList = $activity.find("div"), itemW = $itemList.eq(0) ? $itemList.eq(0).width() : 0;
    $activity.css("width", itemW * $itemList.length + (($itemList.length - 1) * 14) + "px");
    var initX = setInterval(activityMove, 3000);
    $activity.hover(function () {
        $activity.stop(false, true);
        clearInterval(initX);
    }, function () {
        initX = setInterval(activityMove, 3000);
    });



});
/*
 底部滚动广告函数
 */
function activityMove() {
    var $activity = $('.activity-nav');
    var $left = $activity.css("margin-left"), nLeft = parseInt($left.replace("px", ""));
    if (Math.abs(nLeft) + 1024 + 100 < $activity.width()) {
        $activity.animate({
            "margin-left": nLeft - 1024 + "px"
        }, 1000);
    } else {
        $activity.animate({
            "margin-left": "0px"
        }, 'fast');
    }
}
/*
 *@param top{int} 在登录框中出现的高度
 *@param message{String} 提示内容
 */
function showLogTip(top, message) {
    $('.login_tip').stop(true, true).css({
        "opacity": 0,
        "top": "250px"
    }).text("").html('<span class="login_point"></span>').removeClass("hidden").append(message).animate({
            "top": top + "px",
            "opacity": 1
        }, 300);
    var hideTip = function () {
        $('.login_tip').animate({
            "opacity": 0
        }, 300, function () {
            $(this).addClass("hidden").text("");
        });
    };
    setTimeout(hideTip, 2000);
}

function showTxtTip(e, msg) {
    var x = e.pageX, y = e.pageY;
    var $nav = $('<div id="tip"></div>').text(msg);
    $nav.css({position: "absolute", top: y - 20 + "px", left: x + 10 + "px", border: "1px solid", "background-color": "#fff", "z-index": "500", "border-radius": "3px", "padding": "0 5px"});
    $("body").append($nav);
}

/**/
